package com.liqw.auto.listener;

import com.liqw.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;

// ======================== 事件监听器 ========================
@Slf4j
@Component
public class CustomEventListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ServletWebServerInitializedEvent) {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.ServletWebServerInitializedEvent") + "CustomEventListener - ServletWebServerInitializedEvent");
        } else if (event instanceof ContextRefreshedEvent) {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.ContextRefreshedEvent") + "CustomEventListener - ContextRefreshedEvent : 上下文刷新完成");
        } else if (event instanceof ApplicationStartedEvent) {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.ApplicationStartedEvent") + "CustomEventListener - ApplicationStartedEvent");
        } else if (event instanceof AvailabilityChangeEvent) {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.AvailabilityChangeEvent") + "CustomEventListener - AvailabilityChangeEvent");
        } else if (event instanceof ApplicationReadyEvent) {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.ApplicationReadyEvent") + "CustomEventListener - ApplicationReadyEvent");
        } else if (event instanceof ContextStoppedEvent) {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.ContextStoppedEvent") + "CustomEventListener - ContextStoppedEvent");
        } else if (event instanceof ContextClosedEvent) {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.ContextClosedEvent") + "CustomEventListener - ContextClosedEvent");
        } else {
            System.out.println(Constant.getStepStr("CustomEnvPostProcessor.default") + "CustomEventListener - default: " + event.getSource());
        }
    }
}